<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      :inline="true"
      label-width="100px"
    >
      <el-form-item>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['raffle:control:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="奖品ID" align="center" prop="awardId" fixed />
      <el-table-column label="抽奖活动ID" align="center" prop="prizeId" />
      <el-table-column label="活动名称" align="center" prop="prizeName" />
      <el-table-column label="头像" align="center" prop="userAvatar" width="70px">
        <template #default="scope">
          <el-avatar :src="scope.row.userAvatar" />
        </template>
      </el-table-column>
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="操作" align="center" min-width="160">
        <template #default="scope">
          <el-button
            type="danger"
            plain
            @click="deleteConfirmer('delete', scope.row)"
            v-hasPermi="['raffle:control:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <CreateForm ref="formRef" @success="getList" />
</template>

<script lang="ts" setup>
import { getallconfirmerlist, deletefirmer} from '@/api/raffle/control'
import CreateForm from './CreateForm.vue'

defineOptions({ name: 'RaffleControl' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const delParams = reactive({
  useId: '',
  prizeId: '',
})

/** 查询全部指定中奖者列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await getallconfirmerlist()
    console.log(data)
    list.value = data
  } finally {
    loading.value = false
  }
}


/** 添加指定中奖者操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 修改操作 */
const deleteFormRef = ref()
const deleteConfirmer = async (type: string, row:any) => {
  delParams.prizeId = row.prizeId;
  delParams.useId = row.userId;
  try {
    await deletefirmer(deleteConfirmer)
  } finally {
    getList()
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
